<template>
	<div>
		<!-- 面包屑导航区域 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>系统管理</el-breadcrumb-item>
			<el-breadcrumb-item>角色管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 卡片视图区域 -->
		<el-card class="box-card">
			<!-- 搜索与添加区域 -->
			<el-form :inline="true" :model="queryInfo" class="demo-form-inline">
				<el-form-item label="角色名称:">
					<el-input v-model="queryInfo.Name"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="onSubmit" icon="el-icon-search" plain>查询</el-button>

				</el-form-item>
				<div>
					<el-form-item>
						<el-button type="success" round @click="addDialogVisible=true" icon="el-icon-circle-plus-outline" plain>添加角色</el-button>
					</el-form-item>
				</div>
			</el-form>
			<!-- 用户列表区域 -->
			<el-table :data="userlist" border stripe>
				<el-table-column label="序号" prop="Id"></el-table-column>
				<el-table-column label="角色名称" prop="Name"></el-table-column>
				<el-table-column label="操作" width="300px">
					<template slot-scope="scope">
						<!-- 模块 -->
						<el-button size="mini" @click="setRole(scope.row)" plain>模块</el-button>
						<!-- 权限 -->
						<el-button type="warning" size="mini" @click="UserById(scope.row.id)" plain>权限</el-button>
						<!-- 修改按钮 -->
						<el-button type="success" size="mini" @click="showEditDialog(scope.row)" plain>修改</el-button>
						<!-- 删除按钮 -->
						<el-button type="danger" size="mini" @click="removeUserById(scope.row)" plain>删除</el-button>

					</template>
				</el-table-column>
			</el-table>
			<!-- 分页区域 -->
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum"
			 :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>
		<!-- 添加用户的对话框 -->
		<el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
			<!-- 内容主体区域 -->
			<el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
				<el-form-item label="角色名称:" prop="Name" label-width="120px">
					<el-input v-model="addForm.Name"></el-input>
				</el-form-item>
			</el-form>
			<!-- 底部区域 -->
			<span slot="footer" class="dialog-footer">
				<el-button @click="addDialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="addRoles">确 定</el-button>
			</span>
		</el-dialog>
		<!-- 修改用户的对话框 -->
		<el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
			<el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
				<el-form-item label="角色名">
					<el-input v-model="editForm.Name"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="editDialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="editRoleInfo">确 定</el-button>
			</span>
		</el-dialog>
		<!-- 模块管理的对话框 -->
		<el-dialog title="你正在设置角色模块" :visible.sync="setRoleDialogVisible" width="50%" @close="setRoleDialogClosed">
			<div>
				<p>当前的角色:{{roleInfo.Name}}</p>
				<p>
					<el-tree :data="rolesList" show-checkbox node-key="Id" :default-checked-keys="value" ref="tree" :props="defaultProps">
					</el-tree>
				</p>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button @click="setRoleDialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="saveRoleInfo">确 定</el-button>
			</span>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				//获取用户列表的参数对象
				queryInfo: {
					Name: '',
					//当前的页数
					page: 1,
					//当前每页显示多少条数据
					limit: 5
				},
				userlist: [],
				total: 0,
				//控制添加用户对话框的显示与隐藏
				addDialogVisible: false,
				// 添加用户的表单数据
				addForm: {
					Name: ''
				},
				//添加表单的验证规则对象
				addFormRules: {
					Name: [{
						required: true,
						message: '请输入用户名',
						tigger: 'blur'
					}]
				},
				//控制修改用户对话框的显示与隐藏
				editDialogVisible: false,
				//查询到用户信息对象
				editForm: {
					Name: ''
				},
				//修改表单的验证规则对象
				editFormRules: {
					Name: [{
						required: true,
						message: '请输入角色名称',
						tigger: 'blur'
					}, ]
				},
				//删除对象
				DeteteForm: {},
				//控制分配角色对话框的显示与隐藏
				setRoleDialogVisible: false,
				//需要被分配角色的用户信息
				roleInfo: {},
				//所有角色的数据列表
				rolesList: [],
				defaultProps: {
					children: 'children',
					label: 'Name'
				},
				value: [],
				Ids: []
			}
		},
		created() {
			this.getRoleList()
		},
		methods: {
			//多条件搜索
			onSubmit() {
				this.getRoleList();
			},
			async getRoleList() {
				const {
					data: res
				} = await this.$http.get('api/Roles/GetRolesList', {
					params: {
						Name: this.queryInfo.Name,
						page: this.queryInfo.page,
						limit: this.queryInfo.limit
					}
				})
				if (res.status !== 200) {
					return this.$message.error('获取用户列表失败！')
				}
				this.userlist = res.data
				this.total = res.total
				console.log(res)
			},
			//监听 pagesize 改变的事件
			handleSizeChange(newSize) {
				//console.log(newSize)
				this.queryInfo.limit = newSize
				this.getRoleList()
			},
			//监听 页码值 改变的事件
			handleCurrentChange(newPage) {
				//console.log(newPage)
				this.queryInfo.page = newPage
				this.getRoleList()
			},
			//监听添加用户对话框的关闭事件
			addDialogClosed() {
				this.$refs.addFormRef.resetFields()
			},
			//点击按钮，添加新用户
			addRoles() {
				this.$refs.addFormRef.validate(async valid => {
					if (!valid) return
					//可以发起添加用户的网络请求
					const {
						data: res
					} = await this.$http.post('api/Roles/AddRoles', this.addForm)
					if (res.code !== 0) {
						this.$message.error(res.msg)
					}
					this.$message.success(res.msg)
					//隐藏添加用户的对话框
					this.addDialogVisible = false
					//重新获取用户列表数据
					this.getRoleList()
				})
			},
			//展示编辑用户的对话框
			async showEditDialog(row) {
				this.editForm = row
				this.editDialogVisible = true
			},
			//监听修改用户对话框的关闭事件
			editDialogClosed() {
				this.$refs.editFormRef.resetFields()
			},
			//修改用户信息并提交
			editRoleInfo() {
				this.$refs.editFormRef.validate(async valid => {
					//console.log(valid)
					if (!valid) return
					//可以发起修改用户的网络请求
					const {
						data: res
					} = await this.$http.post('api/Roles/EditRoles', this.editForm)
					if (res.code !== 0) {
						this.$message.error(res.msg)
					}
					//关闭对话框
					this.editDialogVisible = false
					//刷新数据列表
					this.getRoleList()
					//提示修改成功
					this.$message.success(res.msg)
				})
			},
			//根据Id删除对应的用户信息
			async removeUserById(row) {
				//弹框询问用户是否删除数据
				this.DeteteForm = row;
				const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).catch(err => err)
				if (confirmResult !== 'confirm') {
					return this.$message.info('已取消删除')
				}
				const {
					data: res
				} = await this.$http.post('api/Roles/DeleteRole', this.DeteteForm)
				if (res.code !== 0) {
					return this.$message.error('删除角色失败！')
				}
				this.$message.success('删除角色成功！')
				this.getRoleList()
			},
			//展示模块管理的对话框
			async setRole(row) {
				this.roleInfo = row
				const {
					data: res
				} = await this.$http.get('api/Roles/GetAllModules')
				if (res.code !== 0) {
					this.$message.error("获取全部模块失败")
				}
				this.rolesList = res.data
				this.rolesList = res.data;
				const {
					data: ress
				} = await this.$http.post('api/Roles/GetMyModules', this.roleInfo)
				if (res.code !== 0) {
					return this.$message.error('获取模块列表失败！')
				}
				this.value = ress.data;
				this.setRoleDialogVisible = true
			},
			//点击按钮 ，分配角色
			async saveRoleInfo() {
				this.Ids = this.$refs.tree.getCheckedKeys();
				const {
					data: res
				} = await this.$http.get('api/Roles/EditRM', {
					params: {
						Id: this.roleInfo.Id,
						Value: this.Ids.toString()
					}
				})
				if (res.code !== 0) {
					return this.$message.error(res.msg)
				}
				this.$message.success(res.msg)
				//刷新数据列表
				this.getRoleList()
				//隐藏对话框
				this.setRoleDialogVisible = false
			},
			//监听分配角色对话框的关闭事件
			setRoleDialogClosed() {
				this.value = []
				this.userInfo = {}
			},
			UserById() {
				this.$message.success("待开发！！！");
			}
		}
	}
</script>

<style Lang="less">
</style>
